package tech.abing.spider.task.pipeline.sumuwang;

import java.util.List;

import org.apache.log4j.Logger;

import tech.abing.spider.component.Pipeline;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.task.model.sumuwang.FundDataEntity;
import tech.abing.spider.task.model.sumuwang.FundNavEntity;
import tech.abing.spider.util.SqlUtil;


public class FundDataPipeline implements Pipeline {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2213634404313616550L;

	private static Logger logger = Logger.getLogger(FundDataPipeline.class);
	private static SqlUtil sqlUtil = null;
	
	public FundDataPipeline() {
		if(sqlUtil == null){
			sqlUtil = SqlUtil.getInstance();
			logger.info("数据库连接创建成功！");
		}
	}
	
	@Override
	public void proccessData(List<Proccessable> objectContainer) {
		if(!objectContainer.isEmpty()){//由于在process层把三个实体类的数据都放在同一个容器里面，所以这里不能实行批处理方案
			for (Proccessable proccessable : objectContainer) {
				if(proccessable.getClass().getName().equals(FundDataEntity.class.getName())){
					sqlUtil.insert(sqlUtil.getConn(), (FundDataEntity)proccessable);
				}else if(proccessable.getClass().getName().equals(FundNavEntity.class.getName())){
					sqlUtil.insert(sqlUtil.getConn(), (FundNavEntity)proccessable);
				}
			}
		}
	}

	@Override
	public void destory() {
		if(sqlUtil != null){
			sqlUtil.closeConn();
			sqlUtil = null;
			logger.info("数据库连接关闭成功！");
		}
	}


}
